using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ColorUtils{

	public static Color addColor(Color c1, Color c2){
		float alpha = (c1.a + c2.a)/2;
		Color newC = c1+c2;
		newC.r = Mathf.Round(Mathf.Clamp(newC.r,0f,1f));
		newC.g = Mathf.Round(Mathf.Clamp(newC.g,0f,1f));
		newC.b = Mathf.Round(Mathf.Clamp(newC.b,0f,1f));
		newC.a = alpha;
		return newC;
	}
	
	public static Color filterColor(Color c1, Color c2){
		float alpha = c1.a;
		Color newC = c1;
		newC.r = (c2.r == 1 ? c1.r : 0);
		newC.g = (c2.g == 1 ? c1.g : 0);
		newC.b = (c2.b == 1 ? c1.b : 0);
		newC.a = alpha;
		return newC;
	}
	
	public static Color TrueYellow(){
		return new Color(1f,1f,0f,1f);
	}
	
	public static bool isPrimary(Color c){
		return c == Color.blue || c == Color.green || c == Color.red;
	}
	
	public static bool isSecondary(Color c){
		return c == Color.magenta || c == Color.cyan || c == TrueYellow();
	}
	
	public static List<Color> getPrimaryComponents(Color c){
		List<Color> components = new List<Color>();
		
		if(isPrimary(c)){
			components.Add(c); 	
		}else if(isSecondary(c)){
			if(c == Color.magenta){
				components.Add(Color.blue);
				components.Add(Color.red);
			}else if(c == Color.cyan){
				components.Add(Color.blue);
				components.Add(Color.green);
			}else{
				components.Add(Color.red);
				components.Add(Color.green);
			}
		}else{
			components.Add(Color.blue);
			components.Add(Color.red);
			components.Add(Color.green);
		}
		
		return components;
	}
	
	public static Color addColors(Color[] listColors){
		if(listColors.Length != 1){
			for(int i=0;i<listColors.Length-1;i++){
				listColors[i+1] = addColor(listColors[i],listColors[i+1]);
			}
		}
		
		return listColors[listColors.Length-1];
	}
	
	public static bool equalsIgnoreAlpha(Color c1,Color c2){
		return c1.r == c2.r && c1.g == c2.g && c1.b == c2.b;
	}
	
}
